package com.hanhanxiaochong.zhongxiangchuanmei.view.home.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.bean.TechnicianBean;
import com.hanhanxiaochong.zhongxiangchuanmei.config.CommonAdapter;
import com.hanhanxiaochong.zhongxiangchuanmei.config.ViewHolder;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.glide.GlideImgManager;
import com.hanhanxiaochong.zhongxiangchuanmei.widget.StarBar;
import com.wzgiceman.rxretrofitlibrary.retrofit_rx.http.RetrofitSource;

import java.util.List;

/**
 * Created by RockPhoenix on 2018/5/24.
 */

public class TechnicianAdapter extends CommonAdapter<TechnicianBean> {

    private int selectPosition = -1;

    public TechnicianAdapter(Context context, int layoutId, List<TechnicianBean> datas) {
        super(context, layoutId, datas);
    }

    @Override
    protected void convert(ViewHolder holder, TechnicianBean bean, int position) {
        ImageView avatar = holder.getView(R.id.iv_avatar);
        TextView name = holder.getView(R.id.tv_technician_name);
        StarBar starBar = holder.getView(R.id.tech_star);
        TextView tvScore = holder.getView(R.id.tv_score);
        TextView tvAddress = holder.getView(R.id.tv_tech_address);
        TextView tvCaseNum = holder.getView(R.id.tv_case_num);
        ImageView techSelect = holder.getView(R.id.iv_tech_select);

        GlideImgManager.glideLoader(mContext, RetrofitSource.image_url + bean.getPic(), R.mipmap.img_error, R.mipmap.img_error, avatar, 0);
        name.setText(bean.getName());
        starBar.setStarMark(Float.parseFloat(bean.getScore()));
        tvScore.setText(bean.getScore());
        tvAddress.setText(bean.getProvince() + bean.getCity() + bean.getArea() + bean.getAddresser());
        tvCaseNum.setText(String.format(ResourcesUtils.getString(R.string.case_num), bean.getSt_num()));
        techSelect.setVisibility(selectPosition == position ? View.VISIBLE : View.INVISIBLE);

        holder.itemView.setOnClickListener(v -> {
            selectPosition = position;
            notifyDataSetChanged();
            mOnItemClickListener.onItemClick(this, position);
        });
    }
}
